package com.example.administrator.myapplication;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    Spinner spinner;
    Button addButton,delButton;
    TextView textView;
    EditText editText;
    List<String> list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        spinner=(Spinner)findViewById(R.id.spinner1);
        addButton=(Button)findViewById(R.id.button_add);

        delButton=(Button)findViewById(R.id.button_del);

        editText=(EditText)findViewById(R.id.editText);

        textView=(TextView)findViewById(R.id.textView2);
        list=new ArrayList<String>();
        list.add("teacher");

        list.add("saler");
        list.add("doctor");

        list.add("student");

        ArrayAdapter<String> adapter=new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_spinner_item,list);

        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        spinner.setAdapter(adapter);

        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {

                String text = ((TextView)arg1).getText().toString();
                Toast.makeText(MainActivity.this,text,Toast.LENGTH_SHORT).show();
                textView.setText(text);

            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
        addButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String career=editText.getText().toString();
                list.add(career);

            }
        });
        delButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String career=editText.getText().toString();
                int index=list.indexOf(career);

                if (index==-1){

                    Toast.makeText(MainActivity.this,"无此项",Toast.LENGTH_SHORT).show();


                }else {

                    Toast.makeText(MainActivity.this,"有此项",Toast.LENGTH_SHORT).show();
                    list.remove(list.indexOf(career));
                }

            }
        });


    }
}
